/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.asset.inventory.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fuyao.cloud.asset.entity.AssetCard;
import com.fuyao.cloud.asset.inventory.enums.InventoryTaskStatusEnum;
import com.fuyao.cloud.asset.inventory.process.HandlerInventoryTaskTypeContext;
import com.fuyao.cloud.asset.inventory.process.InventoryTaskTypeFactory;
import com.fuyao.cloud.asset.inventory.service.InventoryTaskAssetService;
import com.fuyao.cloud.common.biz.service.FyBaseServiceImpl;
import com.fuyao.cloud.asset.inventory.entity.InventoryTask;
import com.fuyao.cloud.asset.inventory.mapper.InventoryTaskMapper;
import com.fuyao.cloud.asset.inventory.service.InventoryTaskService;
import com.fuyao.cloud.common.data.resolver.NumberResolver;
import com.fuyao.cloud.space.constant.EdificeConstant;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 任务盘点管理表
 *
 * @author lyhills
 * @date 2023-09-11 12:01:39
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InventoryTaskServiceImpl extends FyBaseServiceImpl<InventoryTaskMapper, InventoryTask> implements InventoryTaskService {

    @Resource
    private HandlerInventoryTaskTypeContext context;
    @Resource
    private InventoryTaskAssetService inventoryTaskAssetService;

    @Override
    public InventoryTask create() {
        InventoryTask inventoryTask = new InventoryTask();
        inventoryTask.setUnid(UUID.randomUUID().toString());
        inventoryTask.setStatus(InventoryTaskStatusEnum.DRAFT.getCode());
        return inventoryTask;
    }

    @Override
    public boolean save(InventoryTask entity) {
        // 设置排序号
        if (entity.getOrderNo() == null || entity.getOrderNo() == 0) {
            Long orderNum = NumberResolver.getNumberValue("InventoryTask", "InventoryTask").getData();
            entity.setOrderNo(orderNum.intValue());
        }
        boolean hasSave = super.save(entity);
        if(hasSave){
            List<AssetCard> assetCardList = generateInventoryTaskListAsset(entity);
            boolean taskAsset = inventoryTaskAssetService.saveOrUpdateInventoryTaskAsset(entity, assetCardList);
            if(taskAsset){
                log.info("盘点清单数据生成成功！");
            }
        }
        return hasSave;
    }

    @Override
    public boolean updateById(InventoryTask entity) {
        InventoryTask oldInventoryTask = this.getById(entity.getId());
        boolean hasUpdate = super.updateById(entity);
        // 类型有变化时重新生成清单数据
        if(hasUpdate && !oldInventoryTask.getType().equals(entity.getType())){
            List<AssetCard> assetCardList = generateInventoryTaskListAsset(entity);
            boolean taskAsset = inventoryTaskAssetService.saveOrUpdateInventoryTaskAsset(entity, assetCardList);
            if(taskAsset){
                log.info("盘点清单数据生成成功！");
            }
        }
        return hasUpdate;
    }

    /**
     * 生成资产清单数据
     */
    private List<AssetCard> generateInventoryTaskListAsset(InventoryTask entity){
        String type = entity.getType();
        InventoryTaskTypeFactory contextInstance = context.getInstance(type);
        return contextInstance.getAssetCardList(entity);
    }

}
